package com.asia5b.a5mall.shop.adapter

import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5bmall.same.databinding.GoodsitemShophomeBinding
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel

import com.asia5b.a5mall.middleware.utils.CurrencyPriceBuilder3
import com.asia5b.a5mall.middleware.utils.startGoodsDetailActivity
import com.asia5b.a5mall.shop.R
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder


class ShopHomeAdapter(list: ArrayList<AbsGoodsModel>) :
        BaseMultiItemQuickAdapter<AbsGoodsModel, BaseDataBindingHolder<*>>(list) {
    init {
        addItemType(0, R.layout.goodsitem_shophome)
        addItemType(1, R.layout.item_shophome_hot)
        addItemType(2, R.layout.item_shophome_hot)
        addItemType(3, R.layout.item_shophome_hot)
    }


    override fun convert(helper: BaseDataBindingHolder<*>, item: AbsGoodsModel) {
        when (item.type) {
            1, 2, 3 -> {
                setTypeView(helper, item)
            }
            else -> {
                setGoodsView(helper, item)
            }
        }
    }

    fun setTypeView(helper: BaseViewHolder, item: AbsGoodsModel) {

        val iv_shophome_icon = helper.getView<ImageView>(R.id.iv_shophome_icon)
        val tv_shophome_typename = helper.getView<TextView>(R.id.tv_shophome_typename)
        when (item.type) {
            AbsGoodsModel.hot -> {
                iv_shophome_icon.setImageResource(R.drawable.ic_hot_yellow)
                tv_shophome_typename.text = ResString.get(R.string.店铺热卖)
            }
            AbsGoodsModel.new -> {
                iv_shophome_icon.setImageResource(R.drawable.ic_new_yellow)
                tv_shophome_typename.text = ResString.get(R.string.新品上架)
            }
            AbsGoodsModel.recommend -> {
                iv_shophome_icon.setImageResource(R.drawable.ic_recommend_yellow)
                tv_shophome_typename.text = ResString.get(R.string.店家推荐)
            }
        }

    }

    fun setGoodsView(helper: BaseDataBindingHolder<*>, item: AbsGoodsModel) {
//        val binding = helper.itemView.getTag(R.id.BaseQuickAdapter_databinding_support) as GoodsitemShophomeBinding
        val binding=helper.dataBinding as GoodsitemShophomeBinding
        binding.model = item

        val tv_shophome_newprice = helper.getView<TextView>(R.id.tv_shophome_newprice)
        CurrencyPriceBuilder3().run {
            textView = tv_shophome_newprice
            price = item.absGoodsPrice
            priceList = item.absGoodsPriceList
            build14_18()
        }
        helper.itemView.setOnClickListener {
            startGoodsDetailActivity(context,item.absGoodsId)
        }
////        helper.setText( R.id.tv_shophome_oldprice, String.format("¥ %.2f 起", item.price))
//        val tv_shophome_oldprice = helper.getView<TextView>(R.id.tv_shophome_oldprice)
//        CurrencyPriceBuilder2().run {
//            textView = tv_shophome_oldprice
//            price = item.price
//            priceList = item.price_list
//            build()
//        }
//        tv_shophome_oldprice.paintFlags = tv_shophome_oldprice.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG;
    }
}